package base.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import base.BasePackage;
import base.Projet;

public class BaseSerializer {
	
	public Projet load(File f) {
		ResourceSet rs = new ResourceSetImpl();
		Resource.Factory.Registry registry =
		rs.getResourceFactoryRegistry();
		Map<String,Object> m = registry.getExtensionToFactoryMap();
		m.put("xmi", new XMIResourceFactoryImpl());
		rs.getPackageRegistry().put(BasePackage.eNS_URI,
		BasePackage.eINSTANCE);
		URI uri = URI.createFileURI(f.getAbsolutePath());
		Resource resource = rs.getResource(uri, true);
		if (resource.isLoaded() && resource.getContents().size() > 0) {
			return (Projet) resource.getContents().get(0);
		}
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println("Projet CAR _ RobotDSML");
		BaseSerializer bs = new BaseSerializer();
		Projet projetcar = bs.load(new File("model/EpuckSearchForLight.xmi"));
		
		try {
			FileWriter output = new FileWriter("src/generated/GENERATED_DSMLROBOT.java");
			BufferedWriter bfw = new BufferedWriter(output);
			BaseVisitor bavard = new BaseVisitor();
			bavard.visit(projetcar, bfw);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (CompilationExeption e) {
			e.printStackTrace();
		}
		
		
	}

}
